/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community;

import cn.ac.nercis.pes.community.support.AbstractFxmlView;
import cn.ac.nercis.pes.community.support.PropertyReaderHelper;
import cn.ac.nercis.pes.community.utils.GuiStore;
import cn.ac.nercis.pes.community.utils.SpringUtils;
import cn.ac.nercis.pes.community.views.login.LoginView;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * Stage初始化完成事件处理
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@Component
public class StageInitializer implements ApplicationListener<ShellApplication.StageReadyEvent> {

    @Override
    public void onApplicationEvent(ShellApplication.StageReadyEvent event) {
        showDefaultView(event.getStage());
    }

    private void showDefaultView(Stage primaryStage){
        try {
            AbstractFxmlView view = SpringUtils.getBean(LoginView.class);
            if (Objects.isNull(GuiStore.getScene())) {
                Scene scene = new Scene(view.getView());
                scene.setFill(Color.TRANSPARENT);
                GuiStore.setScene(scene);
            } else {
                GuiStore.getScene().setRoot(view.getView());
            }
            GuiStore.getStage().setScene(GuiStore.getScene());
            applyEnvPropsToView(primaryStage);
            primaryStage.show();
        } catch (Throwable var2) {
            showErrorAlert(var2);
        }
    }

    private void applyEnvPropsToView(Stage primaryStage) {
        var environment = SpringUtils.getApplicationContext().getEnvironment();
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        PropertyReaderHelper.setIfPresent(environment, "javafx.title", String.class, primaryStage::setTitle);
        environment = SpringUtils.getApplicationContext().getEnvironment();
        PropertyReaderHelper.setIfPresent(environment, "javafx.stage.resizable", Boolean.class, primaryStage::setResizable);
        primaryStage.getIcons().addAll(GuiStore.readIcons());
        primaryStage.sizeToScene();
    }

    private void showErrorAlert(Throwable throwable) {
        log.error("运行错误：",throwable);
        Alert alert = new Alert(Alert.AlertType.ERROR, "运行错误，详细查看日志信息...");
        alert.showAndWait().ifPresent((response) -> Platform.exit());
    }

}